<?php
class category extends CI_Controller
{
    public function __construct(){
        parent::__construct();
        $this->load->model('category_model');
        $this->load->helper(array('form', 'url'));
        $this->load->library('pagination');
    }
    public function index(){
        $parent = '0';
        $data['title'] = "List category";
        $data['path'] = "category/list";

        //css pagination
        $config['full_tag_open'] = '<ul class="tsc_pagination tsc_paginationA tsc_paginationA01">';
        $config['full_tag_close'] = '</ul>';
        $config['prev_link'] = '&lt;';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&gt;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="current"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';

        $config['first_link'] = '&lt;&lt;';
        $config['last_link'] = '&gt;&gt;';
        //end
        $config['uri_segment'] = 4;
        $config['per_page'] = 4;
        $config['total_rows'] = $this->category_model->totalCate();
        $config['base_url'] = base_url().'admin/category/index';
        $this->pagination->initialize($config);
        $start = $this->uri->segment(4);
        $data['listcate'] = $this->category_model->listCate($config['per_page'],$start);
        $data['total']= $this->category_model->totalCate();
        $this->load->view("index",$data);
    }

    public function addCate()
    {
        $data['title'] = "Add category";
        $data['path'] = "category/add";
        $data['errors'] = "";
        //$data['listcate'] = $this->category_model->listCate();
        $data['list_parent'] = $this->category_model->listCate_add();
        if ($this->input->post('submit') != NULL) {
            $this->form_validation->set_rules('name', 'CategoryName', 'required');
            if ($this->form_validation->run() == NULL) {
                $this->load->view("index", $data);
            } else {
                $name = $this->input->post('name');
                $desciption = $this->input->post('desciption');
                $parent = $this->input->post('parent');
                $dataarray = array(
                    'cate_name' => $name,
                    'cate_description' => $desciption,
                    'cate_parent' => $parent
                );
                $checkname = $this->category_model->checkCate_add($name);
                if ($checkname == FALSE) {
                    $data['errors'] = "Danh mục đã tồn tại";
                    $this->load->view("index", $data);
                } else {
                    $this->category_model->addCate($dataarray);
                    redirect(base_url() . "admin/category");
                    exit();
                }
            }
        } else {
            $this->load->view("index", $data);
        }

    }



    public function moveCate(){
        $id = $this->uri->segment(4);
        $data['title'] = "Move Category";
        $data['path'] = "category/move";
        $data['errors'] = "";
        $data['info'] = $this->category_model->getdata($id);
        $data['list_parent'] = $this->category_model->cate_move($id);
        if($this->input->post('submit') != NULL){
            $parent = $this->input->post('category');
            $dataarray = array(
                "cate_parent" => $parent
            );
            $this->category_model->update($id,$dataarray);
            redirect(base_url()."index.php/admin/category");
        }else{
            $this->load->view('index',$data);
        }


    }

    public function updateCate(){
        $id = $this->uri->segment(4);
        $data['title'] = "Edit Category";
        $data['path'] = "category/update";
        $data['errors'] = "";

        $data['info'] = $this->category_model->getdata($id);
        $data['list_parent'] = $this->category_model->cate_move($id);

        if($this->input->post('submit') != NULL){
            $this->form_validation->set_rules('name','CategoryName','required');
            $this->form_validation->set_rules('description','CategoryDescription');
            $this->form_validation->set_rules('parent','CategoryParent');
            if($this->form_validation->run()  == NULL){
                $this->load->view('index',$data);
            }else{
                $name = $this->input->post("name");
                $description = $this->input->post("description");
                $parent = $this->input->post('category');

                $dbarray = array(
                    "cate_name" => $name,
                    "cate_parent" => $parent,
                    "cate_description" => $description
                );
                $checkCatename = $this->category_model->checkCate("cate_name",$name,$id);

                if($checkCatename == FALSE){
                    $data['errors'] = "Category đã tồn tại";
                    $this->load->view('index',$data);
                }
                else{
                    $this->category_model->update($id,$dbarray);
                    redirect(base_url()."index.php/admin/category");
                    exit();
                }
            }
        }else{
            $this->load->view('index',$data);
        }
    }


}